package com.kzw.oa.byxt.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.byxt.model.Subject;
import com.kzw.oa.byxt.service.StudentService;
import com.kzw.oa.byxt.service.SubjectService;
import com.kzw.oa.model.system.AppUser;


@Controller
@Scope("prototype")
public class TongjiStudentAction extends BaseAction {
	
	@Autowired
	private SubjectService subjectService;	
	@Autowired
	private StudentService studentService;	

	/**
	 * 先找到学生，通过学生的userId,找出相应的Subject
	 * 
	 * 返回的json中包含学生的userId,fullname和题目的oid,name
	 */
	public String list() {
		HttpServletRequest request = getRequest();
		int page = Integer.parseInt(request.getParameter("page"));
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		
//		limit:25
		StringBuilder sb = new StringBuilder();
		sb.append("{\"result\":[");
		/**
		 * 找出学生
		 * */
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		filters.add(new StringPropertyFilter("EQI_roleType", "3"));
		List<AppUser> students = studentService.search(filters);
		
		int totalItems = students.size();
		
		filters = ExtjsUtil.getFilters(request);
		int len = students.size();
		int end = start + limit > len ? len : start + limit;
		
		for(int i = start; i < end; i++) {
//		for(AppUser student : students) {
			AppUser student = students.get(i);
			Long userId = student.getUserId();
//			subjectService.findBy(propertyName, value)
			Subject subject = subjectService.findUniqueByHQL("from Subject  where student = ?", student);
			sb.append("{");
			sb.append("\"userId\":");
			sb.append("\""+userId.toString()+"\",");
			sb.append("\"xuehao\":");
			sb.append("\""+student.getUsername()+"\",");
			sb.append("\"name\":"); 
			sb.append("\"" + student.getFullname() + "\"" + ",");
			sb.append("\"subjectName\":");
			if(subject == null) {
				sb.append("null,"); 
			} else {
				sb.append("\"" + subject.getName() + "\"" + ","); 
				sb.append("\"oid\":");
				sb.append("\""+subject.getOid().toString()+"\",");
				sb.append("\"teacherName\":");
				sb.append("\""+subject.getTeacher().getFullname()+"\",");
			}
			
			sb.append("},");
		}
		sb.append("],");
		sb.append("\"totalItems\":" + totalItems + ",\"totalCounts\":" + totalItems + ",\"totalPages\":" +(totalItems/limit)+",\"nextPage\":"+(page+1)+",\"prePage\":"+(page-1)+",");
		sb.append("\"data\":null, \"sucess\":true}");
//		if (ExtjsUtil.isExport(request)) { // 导出报表
//			List<Subject> resultList = subjectService.search(filters);
//			request.setAttribute("__exportList", resultList);
//			return SUCCESS;
//		}
//
//		Page<Subject> page = subjectService.search2(pageRequest, filters);
//		jsonString = new JSON(students).buildWithFilters(1);
		jsonString = sb.toString();
//		jsonString = new JSON(page).buildWithFilters(3);
		return SUCCESS;
	}
	
	

}
